﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using System.Text;
using System.Web.Routing;
using MVCFoto.Model;

namespace MVCFoto.Web.Utility
{
    public static class Html 
    {
        /// <summary>
        /// 取得性别的中文名表示
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="gender"></param>
        /// <returns></returns>
        public static string Gender(this HtmlHelper helper,short gender)
        {

            if (gender == 0)
                return "男";
            else if (gender == 1)
                return "女";
            else
                return "保密";
        }
        /// <summary>
        /// 分页控件
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="itemcount"></param>
        /// <returns></returns>
        public static string Pager(this HtmlHelper helper,int currentpage, int pagesize, int itemcount)
        {
            return Pager(helper, currentpage, pagesize, itemcount, null);
        }
        /// <summary>
        /// 保持原有多行文本格式
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string LineBreaks(this HtmlHelper helper, string text)
        {
            text = helper.Encode(text);
            text = text.Replace("\r\n", "<br/>");
            text = text.Replace(" ", "&nbsp;");
            return text;
        }

        public static string Pager(this HtmlHelper helper, int currentpage, int pagesize, int itemcount, object htmlAttributes)
        {           
            IList<int> pageange = PageRange(currentpage, pagesize, itemcount);
            //只有一页
            if (pageange.Count <= 1) return "";
            StringBuilder builder = new StringBuilder();
            TagBuilder div = new TagBuilder("div");
            div.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            if (currentpage > 1)
                builder.Append(string.Format("<a class=\"n\" href=\"{1}?page={0}\">&nbsp&nbsp<&nbsp&nbsp</a>", currentpage - 1, helper.ViewContext.HttpContext.Request.Url.AbsolutePath));
           

            foreach (int p in pageange)
            {
                if (p == 0)
                    builder.Append("<span>...</span>");
                else if (p != currentpage)
                    builder.Append(string.Format("<a class=\"n\" href=\"{1}?page={0}\">{0}</a>", p, helper.ViewContext.HttpContext.Request.Url.AbsolutePath));
                else
                    builder.Append(string.Format("<span class=\"current\">{0}</span>", p));
            }
            if (currentpage < pageange[pageange.Count - 1])
                builder.Append(string.Format("<a class=\"n\" href=\"{1}?page={0}\">&nbsp&nbsp>&nbsp&nbsp</a>", currentpage + 1, helper.ViewContext.HttpContext.Request.Url.AbsolutePath));
            div.InnerHtml = builder.ToString();
            return div.ToString();
        }       

        static IList<int> PageRange(int currentpage, int pagesize, int itemcount)
        {
            int pagecount = itemcount / pagesize + (itemcount % pagesize > 0 ? 1 : 0);
            IList<int> range = new List<int>();
            //显示邻近的几页
            int near = 4;
            int n = 2 * near - 1;
            int start = 1;
            int end = start + 9;
            if (pagecount > 10)
            {
                if(currentpage <= n)
                {
                    end = n + 3;
                }
                else if (currentpage > n && currentpage <= pagecount - near)
                {
                    start = currentpage - near;
                    end = currentpage + near;
                }
                else if (currentpage > pagecount - near)
                {
                    start = pagecount - 2*near;
                    end = pagecount;
                }   

            }
            else
            {
                end = pagecount;
            }
            //开始页码与第一页差至少一页时，显示1 ...
            if (start > 2)
            {
                range.Add(1);
                range.Add(0);
            }
            //只剩下少于near+1页，直接全部显示
            if (pagecount - currentpage <= near + 1)
                end = pagecount;
            for (; start <= end; start++)
            {
                range.Add(start);
            }
            //最后页与页面总数差1时，显示... n
            if (end < pagecount-1)
            {
                range.Add(0);
                range.Add(pagecount);            
            }
            return range;


        }

        public static List<SelectListItem> Privacy(int privacy)
        {
           return new List<SelectListItem>() { 
                  new SelectListItem(){Text="所有人",Value="0",Selected=(privacy==0)},
                    new SelectListItem(){Text="好友",Value="1",Selected=(privacy== 1)},
                    new SelectListItem(){Text="我自己",Value="2",Selected=(privacy==2)}
            };
        }

        public static string Privacy(this HtmlHelper helper, short privacy)
        {
            switch (privacy)
            {                
                case 1: return "好友可见";
                case 2: return "私有";
                default: return "公开";
            }
        }

        /// <summary>
        /// 显示友好时间
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="from"></param>
        /// <returns></returns>
        public static string TimeSince(this HtmlHelper helper, DateTime from)
        {
            DateTime now = DateTime.Now;
            TimeSpan ts = now - from;
            string afterBefore = "前";
            if (ts.TotalSeconds < 0)
                afterBefore = "后";
            string since = "";
            int days = Math.Abs(ts.Days);
            int hours = Math.Abs(ts.Hours);
            int minutes = Math.Abs(ts.Minutes);

            if (days == 0)
            {
                if (hours == 0)
                {
                    if (minutes == 0)
                    {
                        since = "不到1分钟";
                    }
                    else
                        since = string.Format("{0}分钟", minutes);
                }
                else
                {
                    if (minutes > 0)
                        since = string.Format("{0}小时{1}分钟", hours, minutes);
                    else
                        since = string.Format("{0}小时", hours);
                }
            }
            else if (days >= 1 && days < 30)
            {
                if (hours > 0)
                    since = string.Format("{0}天{1}小时", days, hours);
                else
                    since = string.Format("{0}天", days, hours);
            }
            else if (days > 30 && days < 365)
            {
                int m = days / 30;
                int d = days % 30;
                if (days > 0)
                    since = string.Format("{0}月{1}天", m, d);
                else
                    since = string.Format("{0}月", m);
            }
            else
            {
                since = from.ToString("yyyy年M月d日 tth:mm:ss");
                afterBefore = "";
            }
            return since + afterBefore;
        }
       /// <summary>
        /// 取得缩略图url
       /// </summary>
       /// <param name="helper"></param>
       /// <param name="url">原始url</param>
       /// <param name="size">缩略图大小</param>
       /// <returns></returns>        
        public static string ThumbnailUrl(this HtmlHelper helper, string url, int size)
        {
            if (String.IsNullOrEmpty(url))
                return url;
            url = url.Replace("\\", "/");
          
            url = url.Insert(url.LastIndexOf("/"), "/s{0}");
            url = url.Replace(url.Substring(url.LastIndexOf(".")), ".jpg");
            return String.Format(url, size.ToString());
        }

       
        public static string AbsoluteUrl(this HtmlHelper helper, string url)
        {
            string domain = helper.ViewContext.HttpContext.Request.ServerVariables["SERVER_NAME"];
            if(url.StartsWith("~"))
                url = url.Replace("~",HttpContext.Current.Request.ApplicationPath);

            return string.Format("http://{0}{1}", domain ,url);
        }

        
    }
}
